<?php
//import b8
require_once(SYSTEM_DIR.'/libs/b8-0.4.4/b8.php');
//start an object
$b8 = new b8();
//define a standing rating
define('B8RATING', 0.7);

function learn_by_b8($text, $mode) {
	global $b8;
	$b8->learn($text, $mode);
}

function unlearn_by_b8($text, $mode) {
	global $b8;
	$b8->unlearn($text, $mode);
}

function get_rating_by_b8($text) {
	global $b8;
	//classify
	$rating = $b8->classify($text);
	return $rating;
}

function get_rating_by_user($screen_name){
	//@todo make it done
	return 0;
	
	global $b8;
	$rating=$b8->getRating($screen_name);
	return is_null($rating)?B8RATING:$rating;
}

function set_rating_by_user($username,$rating){
	global $b8;
	$rating=$b8->setRating($username,$rating);
}

function classify_by_b8($username,$rating) {
	//$rating = get_rating_by_b8($text);
	if($username==USER){
		return "ham";
	} else {
		return $rating > get_rating_by_user($username) ? 'spam' : 'ham';
	}	
}

/**
 * generic filters engine
 *
 * a generic engine to filter out tweets using bayesian, pre-defined behaviors and per-user preferences
 *
 * @param string $text the tweet text that to be filtered
 * @return string 'ham' or 'spam'
 * @author felixding
 * @date 2010-02-26
 */
function classify_tweet($screen_name, $text) {
	
	//per-user preferences
	if(classify_tweet_by_screen_name($screen_name) == 'spam') return 'spam';
	
	//bayesian
	if(classify_tweet_by_bayesian($text) == 'spam' ) return 'spam';
	
	//pre-defined behaviors
	if(classify_tweet_by_behaviors($text) == 'spam') return 'spam';
	
	//now we know
	return 'ham';
}

/**
 * classify a tweet by screen name
 *
 * @param string $screen_name
 * @return string 'ham' or 'spam'
 * @author felixding
 * @date 2010-03-16
 */
function classify_tweet_by_screen_name($screen_name) {
	//myself is of course not a spammer!
	$screen_name_rating = ($screen_name == USER) ? 0 : get_rating_by_user($screen_name);
	return $screen_name_rating > THRESHOLD_RATING_FOR_SCREEN_NAME ? 'spam' : 'ham';
}

/**
 * classify a tweet using bayesian
 *
 * @param string $text the tweet text that to be filtered
 * @return string 'ham' or 'spam'
 * @author felixding
 * @date 2010-03-16
 */
function classify_tweet_by_bayesian($text) {
	$bayesian_rating = get_rating_by_b8($text);
	return $bayesian_rating > THRESHOLD_RATING_FOR_BAYESIAN ? 'spam' : 'ham';
}

/**
 * behavior filters engine
 *
 * a generic engine to filter out tweets using pre-defined behaviors
 *
 * @param string $text the tweet text that to be filtered
 * @return String 'ham' or 'spam'
 * @author felixding
 * @date 2010-02-26
 * @lastmodified $lastmodified$
 */
function classify_tweet_by_behaviors($text) {
	$prefix = 'behavior_filter_';
	$behavior_filters = array(
		'remove_pure_retweets_whose_authors_are_followed'
	);
	$behavior_filters_count = count($behavior_filters);
	
	for($i=0;$i<$behavior_filters_count;$i++) {
		$behavior_filter = $prefix.$behavior_filters[$i];
		if($behavior_filter($text) == 'spam') {
			return 'spam';
		}
	}
	
	return 'ham';
}

/**
 * a behavior filters that remove pure retweets whose authors have been followed
 *
 * @param string $text the tweet text that to be filtered
 * @return String 'ham' or 'spam'
 * @author felixding
 * @date 2010-02-26
 */
function behavior_filter_remove_pure_retweets_whose_authors_are_followed($text) {
	//is the tweet retweet?
	if(substr($text, 0, 2) != 'RT') {
		return 'ham';
	}
	
	//is the author followed?
	//@todo maybe I should leave the function to zhuangbiaowei :p
	//preg_match('/@([a-zA-Z0-9])/$', $text, $matches);
	return 'ham';
}

function trim_msg($text){
	$text=preg_replace ('#(\w+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)(?<![.,])#is','',$text);
	$text=preg_replace ('#rt(?<![.,])#is','',$text);
	$text=preg_replace ('#@([a-z_A-Z0-9]+)(?<![.,])#is','',$text);
	$text=preg_replace ('#\#([a-z_A-Z0-9]+)(?<![.,])#is','',$text);	
	return $text;
}

function analysis_msg($text){
	$msg_array=array();
	preg_match_all('#(\w+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)(?<![.,])#is',$text,$out);
	$msg_array["url"]=count($out[0]);
	preg_match_all('#rt(?<![.,])#is',$text,$out);
	$msg_array["rt"]=count($out[0]);
	preg_match_all('#@([a-z_A-Z0-9]+)(?<![.,])#is',$text,$out);
	$msg_array["user"]=count($out[0]);
	preg_match_all('#\#([a-z_A-Z0-9]+)(?<![.,])#is',$text,$out);
	$msg_array["tag"]=count($out[0]);
	if($msg_array["rt"]>0){
		if(stripos($text,"rt")==0){
			$loca=strripos($text,"//");
			if($loca>0){
				$real_msg=substr($text,$loca+2);
				$prev_msg=substr($text,0,$loca);			
			} else {
				$real_msg="";
				$prev_msg=$text;
			}
		} else {
			$loca=stripos($text,"rt");
			if($loca>0){
				$real_msg=substr($text,0,$loca);
				$prev_msg=substr($text,$loca+2);
			} else {
				$real_msg=$text;
			}
		}
	} else {
		$real_msg=$text;
	}
	$msg_array["real_msg"]=trim_msg($real_msg);
	$msg_array["prev_msg"]=trim_msg($prev_msg);
	return $msg_array;
}

function tweet_filter($timeline,$filter){
	$tweets=array();
	foreach($timeline as $msg){
		$msg_array=analysis_msg($msg['text']);
		if($msg_array['real_msg']!=""){
			$rating_1 = get_rating_by_b8($msg_array['real_msg']);
		} else {
			$rating_1 = 1;
		}
		if($msg_array['prev_msg']!=""){
			$rating_2 = get_rating_by_b8($msg_array['prev_msg']);
		} else {
			$rating_2 = 1;
		}
		if($rating_2==1){
			$rating=$rating_1;
		} else {
			$rating=($rating_1+$rating_2)/2;
		}	
		$rating=$rating-$msg_array["url"]*0.05;
		$rating=$rating+$msg_array["rt"]*0.02;
		$rating=$rating+$msg_array["user"]*0.02;
		$rating=$rating-$msg_array["tag"]*0.05;
		$ham_or_spam = classify_by_b8($screen_name,$rating);
		if($filter == $ham_or_spam or $filter=="all"){
			$msg["rating"]=$rating;
			$msg["ham_or_spam"]=$ham_or_spam;
			$tweets[]=$msg;
		}
	}
	return $tweets;
}
?>